package com.qf.osmproduct.product.xxljob;

/**
 * 天--道--酬--勤
 *
 * @author LiangGaoQiang
 * @ClassName UserSearchHotKeysCleanTask
 * @Date: 2025/6/11 10:56
 * @Description: 描述
 */

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qf.common.core.entity.UserSearchHistory;
import com.qf.osmproduct.product.mapper.UserSearchHistoryMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 定时任务：每隔六个月将当前用户的搜索记录进行逻辑删除，防止用户热词记录表中的数据过大，导致
 *
 * @author GaoQiangLiang
 */
@Component
public class UserSearchHotKeysCleanTask {


    @Resource
    private UserSearchHistoryMapper userSearchHistoryMapper;


    /**
     * 定时任务：每隔六个月将当前用户的搜索记录进行逻辑删除，防止用户热词记录表中的数据过大，导致
     * 搜索记录表数据过大，导致查询效率降低，因此需要定时清理
     */
    @XxlJob("cleanUserHotKeys")
    public void cleanUserHotKeys() {
        // 获取六个月前的 LocalDateTime
        LocalDateTime sixMonthAgo = LocalDateTime.now().minusMonths(6);

        // 构建查询条件：使用 LocalDateTime 类型进行比较
        userSearchHistoryMapper.update(Wrappers.<UserSearchHistory>lambdaUpdate()
                .set(UserSearchHistory::getIsDeleted, 1)
                .eq(UserSearchHistory::getIsDeleted, 0)
                .lt(UserSearchHistory::getSearchTime, sixMonthAgo));
    }
}
